package com.kun.index.controller;


import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.index.entity.Advertisement;
import com.kun.index.entity.Comment;
import com.kun.index.entity.Inform;
import com.kun.index.service.impl.AdvertisementServiceImpl;
import com.kun.index.service.impl.AttachServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-04-05
 */
@RestController
@RequestMapping("/index/advertisement")
public class AdvertisementController {
    @Autowired
    private AdvertisementServiceImpl advertisementService;

    @GetMapping("/queryAllAdvertisements")
    @ApiOperation("查询所有广告")
    public ResultDto queryAllAdvertisements(@RequestParam(name = "pageIndex", required=false)Integer pageIndex,
                                            @RequestParam(name = "auditStatue", required=false)Boolean auditStatue,
                                            @RequestParam(name = "queryTitleLike", required=false)String queryTitleLike,
                                            @RequestParam(name = "pageSize", required=false)Integer pageSize){

        HashMap<String, Object> map = new HashMap<>();
        if (pageIndex != null) {
            map.put("pageIndex", pageIndex);
        }
        if (auditStatue != null) {
            map.put("auditStatue", auditStatue);
        }
        if (queryTitleLike != null) {
            map.put("queryTitleLike", queryTitleLike);
        }
        if (pageSize != null) {
            map.put("pageSize", pageSize);
        }
        Map<String, Object> resultMap = advertisementService.queryAdvertisements(map);
        if (((List<Advertisement>) resultMap.get("advertisements")).size() > 0) {
//            for (Advertisement advertisement : ((List<Advertisement>) resultMap.get("advertisements"))) {
//            }

            return ResultDto.success(resultMap);
        } else {
            return ResultDto.fail(Constants.CODE_301, "数据为空,请重新添加数据");
        }

    }

    @GetMapping("/updateAdvisementAuditStatue")
    @ApiOperation("更新广告审核状态")
    public ResultDto updateAdvisementAuditStatue(@RequestParam("id") Integer advisementId){
        Advertisement advertisement = advertisementService.queryAdvertisementById(advisementId);
        advertisement.setAuditStatue((advertisement.getAuditStatue()==1)?0:1);
        Integer num = advertisementService.updateAdvertisement(advertisement);
        if (num>0){
            return ResultDto.success("更新成功");
        }else {
            return ResultDto.fail("更新失败");
        }
    }


    @PostMapping("/addAdvertisement")
    @ApiOperation("新增广告")
    public ResultDto addAdvertisement(@RequestBody Advertisement advertisement){
        Integer id = advertisementService.addAdvertisement(advertisement);
        if (id!=null){
            return ResultDto.success("新增广告成功");
        }else{
            return ResultDto.fail("新增广告失败");
        }
    }

    @GetMapping("/queryAdvertisementById")
    @ApiOperation("通过id查询广告信息")
    public ResultDto queryAdvertisementById(@RequestParam(name = "id", required=true)Integer advertisementId){
        Advertisement advertisement = advertisementService.queryAdvertisementById(advertisementId);
        if (advertisement!=null){
            return ResultDto.success(advertisement);
        }else{
            return ResultDto.fail(Constants.CODE_302,"数据查询不到,请重新查找数据");
        }


    }


    @PostMapping("/batchDeleteAdvertisement")
    @ApiOperation("批量删除广告")
    public ResultDto batchDeleteInform(@RequestBody List<Integer> ids){
        int num = advertisementService.batchDeleteInform(ids);
        if (num>0){
            return ResultDto.success("删除广告成功");
        }else{
            return ResultDto.fail("删除广告失败");
        }


    }

    @PostMapping("/deleteAdvertisement")
    @ApiOperation("删除广告")
    public ResultDto deleteAdvertisement(@RequestBody Advertisement advertisement){
        int num = advertisementService.deleteAdvertisement(advertisement);
        if (num>0){
            return ResultDto.success("删除广告成功");
        }else{
            return ResultDto.fail("删除广告失败");
        }
    }

    @PostMapping("/updateAdvertisement")
    @ApiOperation("编辑广告")
    public ResultDto updateAdvertisement(@RequestBody Advertisement advertisement){
        //更新博客信息
        int num = advertisementService.updateAdvertisement(advertisement);

        if (num>0){
            return ResultDto.success("修改通知成功");
        }else{
            return ResultDto.fail("修改通知失败");
        }


    }



}

